package hope.qiniu.pic.qiniu;


/**
 * ip从string转成byte
 *
 */
public class IpConventByte 
{
    public static void main(String[] args) throws Exception {
        byte [] a = ipv4Address2BinaryArray("192.168.1.1");
        String address = binaryArray2Ipv4Address(a);
        System.out.println(address);
    }

    /** 将给定的字节数组转换成IPV4的十进制分段表示格式的ip地址字符串 */
    public static String binaryArray2Ipv4Address(byte[] addr) {
        String ip = "";
        for (int i = 0; i < addr.length; i++) {
            ip += (addr[i] & 0xFF) + ".";
        }
        return ip.substring(0, ip.length() - 1);
    }

    /** 将给定的用十进制分段格式表示的ipv4地址字符串转换成字节数组 */
    public static byte[] ipv4Address2BinaryArray(String ipAdd) {
        byte[] binIP = new byte[4];
        String[] strs = ipAdd.split("\\.");
        for (int i = 0; i < strs.length; i++) {
            binIP[i] = (byte) Integer.parseInt(strs[i]);
        }
        return binIP;
    }
    
}
